package com.example.sports_meet.subject;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.MyDbHelper;
import com.example.sports_meet.R;

import java.util.Objects;

public class subject_mod extends AppCompatActivity {
    private Button button;
    private Button button_mod;
    private EditText editText1;
    private EditText editText2;
    private MyDbHelper myDbHelper;
    private Spinner spinner;
    private String selectedItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject_mod);
        Objects.requireNonNull(getSupportActionBar()).hide();

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        button_mod = findViewById(R.id.button_mod);
        button_mod.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editText1 = findViewById(R.id.editText1); //要修改的项
                editText2 = findViewById(R.id.editText2);
                myDbHelper = new MyDbHelper(subject_mod.this, "mydb", null, 1);
                SQLiteDatabase db = myDbHelper.getWritableDatabase();
                if (selectedItem.equals("项目名称")) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("fname", editText2.getText().toString());
                    db.update("form", contentValues, "fname=?", new String[]{editText1.getText().toString()});
                    Toast.makeText(subject_mod.this, "修改成功", Toast.LENGTH_SHORT).show();
                }
                db.close();
            }
        });

        spinner = findViewById(R.id.spinner);
        String[] items = new String[]{"项目名称"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                selectedItem = adapterView.getItemAtPosition(i).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                Toast.makeText(subject_mod.this, "请选择一项", Toast.LENGTH_SHORT).show();
            }
        });

    }
}